package com.ruoyi.business.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.domain.BusinessTradeRecord;
import com.ruoyi.business.dto.OpenPositionSemaphoreDto;
import com.ruoyi.business.enumType.PSIceOrderState;
import com.ruoyi.business.exception.TradeException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BusinessTradeConfigMapper;
import com.ruoyi.business.domain.BusinessTradeConfig;
import com.ruoyi.business.service.IBusinessTradeConfigService;

/**
 * 交易参数设置Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-17
 */
@Service
public class BusinessTradeConfigServiceImpl implements IBusinessTradeConfigService {

    private static final Logger logger = LoggerFactory.getLogger(BusinessTradeConfigServiceImpl.class);


    @Autowired
    private BusinessTradeConfigMapper businessTradeConfigMapper;

    /**
     * 查询交易参数设置
     *
     * @param id 交易参数设置主键
     * @return 交易参数设置
     */
    @Override
    public BusinessTradeConfig selectBusinessTradeConfigById(Long id) {
        BusinessTradeConfig businessTradeConfig = businessTradeConfigMapper.selectBusinessTradeConfigById(id);
        if(Objects.nonNull(businessTradeConfig)&& StringUtils.isNotEmpty(businessTradeConfig.getOpenPositionSemaphoreStr())){
            businessTradeConfig.setOpenPositionSemaphore(JSON.parseObject(businessTradeConfig.getOpenPositionSemaphoreStr(), OpenPositionSemaphoreDto.class));
        }
        return businessTradeConfig;
    }

    /**
     * 查询交易参数设置列表
     *
     * @param businessTradeConfig 交易参数设置
     * @return 交易参数设置
     */
    @Override
    public List<BusinessTradeConfig> selectBusinessTradeConfigList(BusinessTradeConfig businessTradeConfig) {
        return businessTradeConfigMapper.selectBusinessTradeConfigList(businessTradeConfig);
    }

    /**
     * 新增交易参数设置
     *
     * @param businessTradeConfig 交易参数设置
     * @return 结果
     */
    @Override
    public int insertBusinessTradeConfig(BusinessTradeConfig businessTradeConfig) {
        if (businessTradeConfig.getTradeHedge()) {
            Long tradeTimesLimit = businessTradeConfig.getTradeTimesLimit();
            int i = Integer.remainderUnsigned(Math.toIntExact(tradeTimesLimit), 2);
//            if (i != 0) {
//                throw new TradeException("开启对冲下单后，开仓次数需要为2的倍数");
//            }
        }
        if (Objects.nonNull(businessTradeConfig.getTradeVolumePerLimit())) {
            Long tradeVolumePerLimit = businessTradeConfig.getTradeVolumePerLimit();
            if (tradeVolumePerLimit > 1000000) {
                throw new TradeException("单笔开仓股数不得超过1000000");
            }
        }
        if(Objects.isNull(businessTradeConfig.getDynamicStopProfitRate())&&Objects.isNull(businessTradeConfig.getStaticStopProfitRate())){
            throw new TradeException("动态止盈和静态止盈不可都为空");
        }
        businessTradeConfig.setCreateTime(DateUtils.getNowDate());
        businessTradeConfig.setOpenPositionSemaphoreStr(JSON.toJSONString(businessTradeConfig.getOpenPositionSemaphore()));
        return businessTradeConfigMapper.insertBusinessTradeConfig(businessTradeConfig);
    }

    /**
     * 修改交易参数设置
     *
     * @param businessTradeConfig 交易参数设置
     * @return 结果
     */
    @Override
    public int updateBusinessTradeConfig(BusinessTradeConfig businessTradeConfig) {
        return businessTradeConfigMapper.updateBusinessTradeConfig(businessTradeConfig);
    }

    /**
     * 批量删除交易参数设置
     *
     * @param ids 需要删除的交易参数设置主键
     * @return 结果
     */
    @Override
    public int deleteBusinessTradeConfigByIds(Long[] ids) {
        return businessTradeConfigMapper.deleteBusinessTradeConfigByIds(ids);
    }

    /**
     * 删除交易参数设置信息
     *
     * @param id 交易参数设置主键
     * @return 结果
     */
    @Override
    public int deleteBusinessTradeConfigById(Long id) {
        return businessTradeConfigMapper.deleteBusinessTradeConfigById(id);
    }

    @Override
    public void takePositionCheck() {


    }
}
